<template>
  <view v-if="list.length" class="mt-tabs">
    <scroll-view class="mt-tabs-scroll" scroll-with-animation :scroll-left="leftNum" :show-scrollbar="false" scroll-x>
      <view v-for="(item, index) in list" :key="index" class="mt-tabs-item" :class="{ active: current === index }" @click="handleChange(index)">
        <slot :row="item">
          <text>{{ item[label] }}</text>
        </slot>
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
const props = defineProps({
  list: {
    type: Array,
    default: () => []
  },
  label: {
    type: String,
    default: 'name'
  }
});

const leftNum = ref(0);
// 当前选中的索引
const current = defineModel({ type: Number, default: 0 });
function handleChange(index) {
  current.value = index;
}
</script>

<style lang="scss" scoped>
.mt-tabs {
  .mt-tabs-scroll {
    white-space: nowrap;
  }
  .mt-tabs-item {
    position: relative;
    display: inline-block;
    font-size: 28rpx;
    color: #727687;
    padding-bottom: 10rpx;
    margin-right: 32rpx;
    &:last-child {
      margin-right: 0;
    }
    &.active {
      color: #1484ee;
      &::before {
        content: '';
        position: absolute;
        left: 50%;
        bottom: 0;
        width: 46rpx;
        height: 4rpx;
        background-color: #1484ee;
        border-radius: 4rpx;
        transform: translateX(-50%);
      }
    }
  }
}
</style>
